//
//  SFGroupTableView.m
//  Test
//
//  Created by xueshan1 on 2019/3/20.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "SFGroupTableView.h"
#import "SFGroupTableViewCell.h"
@interface SFGroupTableView ()<UITableViewDelegate,UITableViewDataSource,SFGroupTableViewCellDelegate>

@end

@implementation SFGroupTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self registerClass:[SFGroupTableViewCell class] forCellReuseIdentifier:@"SFGroupTableViewCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(numberOfSectionsInGroupTableView:)]) {
        return [self.groupDelegate numberOfSectionsInGroupTableView:self];
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat rowCount = 0;
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:numberOfRowsInSection:)]) {
        rowCount = [self.groupDelegate groupTableView:self numberOfRowsInSection:indexPath.section];
    }
    CGFloat rowHeight = 0;
    for (int i = 0; i<rowCount; i++) {
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
        if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:heightForRowAtIndexPath:)]) {
            rowHeight += [self.groupDelegate groupTableView:self heightForRowAtIndexPath: newIndexPath];
        }
    }
    
    NSLog(@"rowHeight: %f",rowHeight);
    return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SFGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SFGroupTableViewCell"];
    cell.tableView.separatorStyle = self.separatorStyle;
    cell.cellDelegate = self;
    cell.tag = indexPath.section + 100;
    
    cell.contentView.layer.cornerRadius = 10;
    cell.contentView.layer.masksToBounds = YES;
//    cell.clipsToBounds = YES;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:heightForHeaderInSection:)]) {
        return [self.groupDelegate groupTableView:self heightForHeaderInSection:section];
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:viewForHeaderInSection:)]) {
        return [self.groupDelegate groupTableView:self viewForHeaderInSection:section];
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:heightForFooterInSection:)]) {
        return [self.groupDelegate groupTableView:self heightForFooterInSection:section];
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:viewForFooterInSection:)]) {
        return [self.groupDelegate groupTableView:self viewForFooterInSection:section];
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    SFGroupTableViewCell *groupCell = (SFGroupTableViewCell *)cell;
    [groupCell.tableView reloadData];
    
}

#pragma mark - GroupCell delegate
- (NSInteger)groupTableViewCell:(SFGroupTableViewCell *)groupCell numberOfRowsInSection:(NSInteger)section{
        
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:numberOfRowsInSection:)]) {
        return [self.groupDelegate groupTableView:self numberOfRowsInSection:groupCell.tag - 100];
    }
    return 0;
}
- (CGFloat)groupTableViewCell:(SFGroupTableViewCell *)groupCell heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:heightForRowAtIndexPath:)]) {
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:groupCell.tag - 100];
//        NSLog(@"sec: %ld row:%ld  heightForRow:%f",newIndexPath.section,newIndexPath.row, [self.groupDelegate groupTableView:self heightForRowAtIndexPath:newIndexPath]);

        return [self.groupDelegate groupTableView:self heightForRowAtIndexPath:newIndexPath];
    }
    return 0;
}
- (UITableViewCell *)groupTablewViewCell:(SFGroupTableViewCell *)groupCell cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:cellForRowAtIndexPath:)]) {
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:groupCell.tag - 100];
        return [self.groupDelegate groupTableView:self cellForRowAtIndexPath:newIndexPath];
    }
    return nil;
}
- (void)groupTableViewCell:(SFGroupTableViewCell *)groupCell didSelectedRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.groupDelegate && [self.groupDelegate respondsToSelector:@selector(groupTableView:didSelectRowAtIndexPath:)]) {
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:groupCell.tag - 100];
        [self.groupDelegate groupTableView:self didSelectRowAtIndexPath:newIndexPath];
    }
}





@end
